

package com.hs.jhzd.controller;

import com.hs.jhzd.entity.Duty;
import com.hs.jhzd.entity.Sgd;
import com.hs.jhzd.entity.Shift;
import com.hs.jhzd.entity.dto.FoodsDto;
import com.hs.jhzd.repository.DutyRepository;
import com.hs.jhzd.repository.SgdRepository;
import com.hs.jhzd.repository.ShiftRepository;
import com.hs.jhzd.utils.DateUtils;
import com.hs.jhzd.utils.enums.DayOfWeekFullEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@RequestMapping("/sgd")
@Controller
public class SgdController {

  @Autowired
  private SgdRepository sgdRepository;

  @Autowired
  private ShiftRepository shiftRepository;

  @Autowired
  private DutyRepository dutyRepository;


//  @RequestMapping("/foods")
//  @ResponseBody
//  public FoodsDto[] foods(@RequestParam(required = true) String jsbh) {
//    List<Foods> foods = foodsRepository.findFoods(jsbh, DateFormatUtils.format(startTime, DateUtils.DATE_FORMAT2),
//        DateFormatUtils.format(endTime, DateUtils.DATE_FORMAT2));
//    if (CollectionUtils.isEmpty(foods)) {
//      return new FoodsDto[0];
//    }
//    FoodsDto[] dtos = getFoodsMenuDtoArr();
//
//    for (Foods food : foods) {
//      int dayOfWeek = DayOfWeekEnum.getValue(food.getDayOfWeek());
//      dtos[dayOfWeek].setDate(food.getDate());
//      dtos[dayOfWeek].setDayOfWeek(food.getDayOfWeek());
//      int dinnerType = food.getDinnerType() - 1;// 一天三餐数据库从1开始，数组从0开始
//      dtos[dayOfWeek].getNames()[dinnerType] = food.getName();
//    }
//    return dtos;
//  }

  /**
   * 生成一个七天菜谱dto的数组.
   *
   * @return 菜谱
   */
  private FoodsDto[] getFoodsMenuDtoArr() {
    FoodsDto[] dtos = new FoodsDto[7];// 一周七天
    for (int i = 0; i < 7; i++) {
      FoodsDto dto = new FoodsDto();
      dto.setNames(new String[3]);// 一天三餐
      dtos[i] = dto;
    }
    return dtos;
  }
  //床铺信息
  @RequestMapping("/bed")
  @ResponseBody
  public List<Sgd> bed(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {
    return sgdRepository.findByJsbhAndJshAndRybjOrderByBedNo(jsbh, jsh, "0");
  }
  //值日信息
  @RequestMapping("/duty")
  @ResponseBody
  public List<Duty> duty(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {
    Date startTime = DateUtils.getMondayOfWeek();
    Date endTime = DateUtils.getSundayOfWeek();
    return dutyRepository.findByJsbhAndJshAndDjsjBetween(jsbh, jsh,startTime,endTime);
  }

  @RequestMapping("/shift")
  @ResponseBody
  public List<Shift> shift(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {
    return shiftRepository.findByJsbhAndJsh(jsbh, jsh);
  }

  @RequestMapping("/todayShift")
  @ResponseBody
  public List<Shift> todayShift(@RequestParam(required = true) String jsbh, @RequestParam(required = true) String jsh) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    String cycle = DayOfWeekFullEnum.getValue(calendar.get(Calendar.DAY_OF_WEEK));
    return shiftRepository.findByJsbhAndJshAndCycle(jsbh, jsh, cycle);
  }

  // private int getDayOfWeek() {
  // Calendar calendar = Calendar.getInstance();
  // int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  // if (dayOfWeek == 0) {
  // dayOfWeek = 7;
  // }
  //
  // // 如果当前时间小于上午9点，取前一天的值班
  // int nowHour = calendar.get(Calendar.HOUR_OF_DAY);
  // if (nowHour <= 9) {
  // // 9点前，且是周一查询上周日的值班信息
  // if (dayOfWeek == 1) {
  // dayOfWeek = 7;
  // } else {
  // dayOfWeek -= 1;
  // }
  // }
  // return dayOfWeek;
  // }

  // private List<Shift> removeDefaultHead(List<Shift> dailyShifts, String jsh) {
  // if (CollectionUtils.isEmpty(dailyShifts)) {
  // return dailyShifts;
  // }
  // boolean independentTime = false;
  // Integer definedNumber = 0;
  // for (Shift d : dailyShifts) {
  // if (StringUtils.equals(d.getJsh(), jsh) && d.getDateAdd() == 0) {
  // independentTime = true;
  // definedNumber++;
  // }
  // }
  // if (!independentTime) {
  // return dailyShifts;
  // }
  //
  // Iterator<Shift> iteratorEntity = dailyShifts.iterator();
  // while (iteratorEntity.hasNext()) {
  // Shift initCountPrisoner = iteratorEntity.next();
  // if (!StringUtils.equals(initCountPrisoner.getJsh(), jsh)) {
  // iteratorEntity.remove();
  // }
  // if (definedNumber > 1 && StringUtils.equals(initCountPrisoner.getJsh(), jsh)) {
  // definedNumber--;
  // iteratorEntity.remove();
  // }
  // }
  // return dailyShifts;
  //
  // }

  // private boolean thisJshHeader(List<Shift> dailyShifts, String jsh) {
  // boolean independentTime = false;
  // for (Shift d : dailyShifts) {
  // if (StringUtils.equals(d.getJsh(), jsh) && d.getDateAdd() == 0) {
  // independentTime = true;
  // }
  // }
  // return independentTime;
  // }
}
